package br.com.controleFrota;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TableRow;

public class ControleDeFrotaActivity extends Activity implements View.OnClickListener {
    
	
	public BaseDados dh;
	public SelectViagem selectViagem;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.dh = new BaseDados(this);
        
        selectViagem = new SelectViagem();
        
        TableRow tbPainel = (TableRow) findViewById(R.id.tbPainel);
        tbPainel.setOnClickListener(this); 
        
        TableRow tbRelatorio = (TableRow) findViewById(R.id.tbRelatorio);
        tbRelatorio.setOnClickListener(this); 

        TableRow tbSinc = (TableRow) findViewById(R.id.tbSinc);
        tbSinc.setOnClickListener(this); 
        
        
    }
    
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	   super.onCreateOptionsMenu(menu);
	   MenuInflater inflater = getMenuInflater();
	   inflater.inflate(R.menu.menu, menu);
	   return true;
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		
		   switch (item.getItemId()) {

		   	   case R.id.preferencias:
		   		   startActivity(new Intent(this, Preferences.class));
				  
				  return true;
			   case R.id.about:
				   startActivity(new Intent(this, Sobre.class)); 
				  return true;
				   
		   }
	   
	   return false;
	}
    
    
    
    @Override
	public void onClick(View v) {
		
		switch (v.getId()) { 
        
			case R.id.tbPainel: 
				abrirPainel(); 
	            break;
	            
			case R.id.tbRelatorio: 
				startActivity(new Intent(this, Relatorio.class)); 
	            break; 
	            
//			case R.id.tbRelatorioLog: 
//				startActivity(new Intent(this, RelatorioLog.class)); 
//	            break;
//	            
//			case R.id.tbMap: 
//				startActivity(new Intent(this, Map.class)); 
//	            break;
//	            
			case R.id.tbSinc: 
				startActivity(new Intent(this, Sinc.class)); 
	            break; 


		        
		}
			
	}
    
    
    public void abrirPainel()
    {
    	
//    	Log.d("TAG", "ABRIR PAINEL");
    	
    	int idViagem = this.viagemRunning();
    	
    	if(idViagem > 0){
    		
//    		Log.d("TAG", "VIAGEM RUNNING");
    		
    		Intent i = new Intent(this, Painel.class);
			i.putExtra("id", idViagem);
			
			startActivity(i);
			
    	}else{
    		
//    		Log.d("TAG", "SELECIONAR VIAGEM");
    		
    		startActivity(new Intent(this, SelectViagem.class));
    		
    	}
    	
		
    }
    
    
    public int viagemRunning()
	{
		
		this.dh = new BaseDados(this);
		
		Cursor viagensIniciadas = dh.selectViagemEmAndamento();
		
		if (viagensIniciadas.moveToFirst() && viagensIniciadas.getCount() > 0) {
			
//			Log.d("TAG", "RETORNA VIAGEM");
			return Integer.parseInt(viagensIniciadas.getString(0));
			
		}else{
			
//			Log.d("TAG", "RETORNA ZERO");
			return 0;
			
		}
		
	}
    
    
}